<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="java.sql.*" import="Connect.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<title>Hammer Time Auctions</title>
</head>
<body>
	<div id="content">
		<%@include file="header.jsp"%>
		<h1>Result</h1>
		<hr></hr>
		<%
			String username = request.getParameter("username");
			//String redirect = "index.jsp";
			// Test below
			String password = request.getParameter("password");
			String retype_password = request.getParameter("retype_password");
			String email = request.getParameter("email");
			String first_name = request.getParameter("first_name");
			String last_name = request.getParameter("last_name");
			String address = request.getParameter("address");
			String city = request.getParameter("city");
			String state = request.getParameter("state");
			String zip_code = request.getParameter("zip_code");
			String telephone = request.getParameter("telephone");
			String hourly_wage = request.getParameter("hourly_wage");
			String level = request.getParameter("level");
			String ssn = request.getParameter("ssn");
			String credit_card_number = "";
			String start_date = "";
			String rating = "";
			String redirect = "index.jsp";
			//if (session.getAttribute("user_type").equals("2")) {
			credit_card_number = request.getParameter("credit_card_number");
			rating = request.getParameter("rating");
			//	}
			//if (session.getAttribute("user_type").equals("1")) {
			start_date = request.getParameter("start_date");
			//}

			// Check if the password is at least 5 characters long and less than
			// or equal 16 characters.
			boolean password_test = password.length() >= 5 && password.length() <= 16;
			// Check if the password and retype password match.
			boolean retype_password_test = password.equals(retype_password);
			// Check if email has an @ character and if there is a period anywhere
			// after the @ character.
			boolean email_test = email.contains("@") && email.indexOf("@") < email.lastIndexOf(".");
			// No tests for these fields yet.
			boolean first_name_test = true;
			boolean last_name_test = true;
			boolean address_test = true;
			boolean city_test = true;
			boolean state_test = true;
			// Check if zip code is an integer.
			boolean zip_code_test = true;
			try {
				int num = Integer.parseInt(zip_code);
			} catch (Exception e) {
				zip_code_test = false;
			}
			// Check if telephone is a long integer.
			boolean telephone_test = true;
			try {
				long num = Long.parseLong(telephone);
			} catch (Exception e) {
				telephone_test = false;
			}
			// Check if credit card number is a 16 digit long integer.
			boolean credit_card_number_test = true;
			//	if(session.getAttribute("user_type").equals("2")){
			if (credit_card_number != null) {
				credit_card_number_test = credit_card_number.length() == 16;
				try {
					long num = Long.parseLong(credit_card_number);
				} catch (Exception e) {
					credit_card_number_test = false;
				}
			}
			//	}
			boolean hourlywage_test = true;
			boolean ssn_test = true;
			//	if(session.getAttribute("user_type").equals("1")){
			// Check if telephone is a long integer.
			hourlywage_test = true;
			if (hourly_wage != null) {
				try {
					float num = Float.parseFloat(hourly_wage);
				} catch (Exception e) {
					hourlywage_test = false;
				}
			}
			// Check if the ssn is 9 characters.
			if (ssn != null) {
				ssn_test = ssn.length() == 9;
				try {
					long num = Long.parseLong(ssn);
				} catch (Exception ee) {
					ssn_test = false;
				}
			}
			//	}

			// Compile the error messages together.
			String errors = "";
			if (!password_test) {
				errors += "* Password must be between 5 and 16 characters, inclusive.<br />";
			}
			// 			if (!retype_password_test) {
			// 				errors += "* The two passwords do not match.<br />";
			// 			}
			if (!first_name_test) {
				errors += "<br />";
			}
			if (!last_name_test) {
				errors += "<br />";
			}
			if (!address_test) {
				errors += "<br />";
			}
			if (!city_test) {
				errors += "<br />";
			}
			if (!state_test) {
				errors += "<br />";
			}
			if (!zip_code_test) {
				errors += "* ZIP code must be an integer.<br />";
			}
			if (!telephone_test) {
				errors += "* Telephone must be an integer. Do not include the '-'s.<br />";
			}
			//		if(session.getAttribute("user_type").equals("2")){
			if (!credit_card_number_test) {
				errors += "* Credit card number must be a 16 digit integer. Do not include '-'s.<br />";
			}
			//		}
			//	if(session.getAttribute("user_type").equals("1")){
			if (!hourlywage_test) {
				errors += "* Hourly Wage must be a digit integer. Do not include '.'s.<br />";
			}
			if (!ssn_test) {
				errors += "* SSN must be a 9 digit integer. Do not include '-'s.<br />";
			}
			//	}

			// If there are any errors abort registration and send the user back to the
			// register page. It also saves non-password type data that the user has submitted.
			//			if (!errors.equals("")) {
			// 				session.setAttribute("register_errors", errors);
			// 				session.setAttribute("username", username);
			// 				session.setAttribute("email", email);
			// 				session.setAttribute("first_name", first_name);
			// 				session.setAttribute("last_name", last_name);
			// 				session.setAttribute("address", address);
			// 				session.setAttribute("city", city);
			// 				session.setAttribute("state", state);
			// 				session.setAttribute("zip_code", zip_code);
			// 				session.setAttribute("telephone", telephone);
			// 				if(session.getAttribute("user_type").equals("2")){
			// 					session.setAttribute("credit_card_number", credit_card_number);
			// 				}
			// 				session.setAttribute("rating", rating);
			// 				session.setAttribute("start_date", start_date);
			// 				session.setAttribute("ssn", ssn);
			//response.sendRedirect("edit_profile.jsp");
			// Test above
			if (username == null) {
				out.println("No username was specified to be deleted.<br /> Did you get here by accident? <br /> You will be redirected to the home page. <br />");
			} else if (!errors.equals("")) {
				out.println("<div class=\"error-text\">" + errors + "</div>");
			} else {

				// Delete the user from all tables.
				java.sql.Connection conn = null;
				try {
					// Connect to the database.
					Class.forName(ConnectionInfo.myJDBCDriver()).newInstance();
					java.util.Properties sysprops = System.getProperties();
					sysprops.put("user", ConnectionInfo.myUserID());
					sysprops.put("password", ConnectionInfo.myPassword());
					conn = java.sql.DriverManager.getConnection(ConnectionInfo.myURL(), sysprops);

					// Update the information for the specified user.
					java.sql.Statement statement = conn.createStatement();
					//int employee = statement.executeUpdate("DELETE FROM employees WHERE username ='" + username + "'");

					// Determine the type of user.
					String user_type = request.getParameter("user_type");
					int employee = 0;
					int customer = 0;
					int user = 0;
					// Retrieve the submitted information.
					username = "'" + username + "'";
					password = "'" + request.getParameter("password") + "'";
					email = "'" + request.getParameter("email") + "'";
					first_name = "'" + request.getParameter("first_name") + "'";
					last_name = "'" + request.getParameter("last_name") + "'";
					address = "'" + request.getParameter("address") + "'";
					city = "'" + request.getParameter("city") + "'";
					state = "'" + request.getParameter("state") + "'";
					zip_code = "'" + request.getParameter("zip_code") + "'";
					telephone = "'" + request.getParameter("telephone") + "'";
					start_date = "'" + request.getParameter("start_date") + "'";
					hourly_wage = "'" + request.getParameter("hourly_wage") + "'";
					level = "'" + request.getParameter("level") + "'";
					credit_card_number = "'" + request.getParameter("credit_card_number") + "'";
					rating = "'" + request.getParameter("rating") + "'";
					ssn = "'" + request.getParameter("ssn") + "'";
					// Perform update queires based on user type.
					user = statement.executeUpdate("UPDATE users SET password = " + password + " ,email =" + email + " ,first_name = " + first_name
							+ " ,last_name = " + last_name + " ,address = " + address + " ,city = " + city + " ,state = " + state + " ,zip_code = "
							+ zip_code + " ,telephone = " + telephone + " WHERE username =" + username);
					if (user_type.equalsIgnoreCase("employee")) {
						employee = statement.executeUpdate("UPDATE employees SET start_date = " + start_date + " ,hourly_wage = " + hourly_wage
								+ " ,level = " + level + " ,ssn = " + ssn + " WHERE username = " + username);
					} else if (user_type.equalsIgnoreCase("customer")) {
						customer = statement.executeUpdate("UPDATE customers SET credit_card_number = " + credit_card_number + " , rating = "
								+ rating + " WHERE username = " + username);
					}

					// Output confirmation messages based on the results of the udpates.
					if (employee > 0) {
						out.println(request.getParameter("username") + "'s information has been updated.<br />");
						redirect = "manage_employees.jsp";
					} else if (customer > 0) {
						out.println(request.getParameter("username") + "'s information has been updated.<br />");
						redirect = "manage_customers.jsp";
					} else if (user == 0) {
						out.println(request.getParameter("username") + " does not exist in the database.");
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						conn.close();
					} catch (Exception ee) {
					}
				}
			}
			out.println("You will be redirected in 5 seconds.<br /><a href=\"" + redirect + "\">Click here if you can't wait!</a>");
			response.setHeader("Refresh", "5; " + redirect);
		%>
	</div>
	<hr></hr>
	<%@include file="footer.jsp"%>
</body>
</html>